import pyspacemouse
import time

def main():
    # 打开设备
    success = pyspacemouse.open()
    if not success:
        print("Failed to open SpaceMouse device. Please check the device connection and permissions.")
        return

    print("SpaceMouse device opened successfully. Reading data...")

    try:
        while True:
            # 读取设备状态
            state = pyspacemouse.read()

            # 获取 6 轴状态
            x, y, z = state.x, state.y, state.z
            roll, pitch, yaw = state.roll, state.pitch, state.yaw

            # 获取按键状态
            button_0 = state.buttons[0]  # 按键 0 的状态
            button_1 = state.buttons[1]  # 按键 1 的状态

            # 打印状态
            print(f"X: {x:.2f}, Y: {y:.2f}, Z: {z:.2f}, Roll: {roll:.2f}, Pitch: {pitch:.2f}, Yaw: {yaw:.2f}")
            print(f"Button 0: {button_0}, Button 1: {button_1}")

            # 稍作延时，避免过快刷新
            time.sleep(0.1)

    except KeyboardInterrupt:
        print("Exiting...")

if __name__ == "__main__":
    main()